#include <stdio.h>
#include <malloc.h>
struct node
{
	int data;
	struct node* pre;
	struct node* next;
};
void Joseph_problem(int i,int j,int k);
int main(void)
{
	Joseph_problem(7,2,3);
	return 0;
}
void Joseph_problem(int i,int j,int k)
{
	struct node* L = NULL,* l = NULL;	
	int a = 1;
	for(;a <= i;a++)
	{
		if(L == NULL)
		{
			L = (struct node*)malloc(sizeof(struct node));
			L->data = a;
			l = L;
		}
		else
		{
			l->next = (struct node*)malloc(sizeof(struct node));
			l->next->data = a;
			l->next->pre = l;
			l = l->next;
		}
	}
	L->pre = l;
	l->next = L;
	l = L;
	for(a = 1;a <= j;a++)
	{
		l = l->next;
	}
	a = 1;
	struct node* temp;
	while(L->next != L)
	{
		if(k == a)
		{
			temp = L->next;
			L->next = L->next->next;
			L->next->pre = L;
			printf("%d\n",temp->data);
			free(temp);
			a = 1;
		}
		else
		{
			a++;
		}
		L = L->next;
	}
	printf("%d\n",L->data);
	free(L);
}
